package local.blueheart.data.springdatademo.util;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class StringUtil {

    private static final String PREFIX = "CN=";


    public static String formatServerName(String serverName, String suffix) {
        if (serverName.indexOf(PREFIX) != -1 && serverName.lastIndexOf(suffix) != -1) {
            String a = serverName.substring(serverName.indexOf(PREFIX) + PREFIX.length(), serverName.lastIndexOf(suffix));
            //String b = a.substring(0,serverName.lastIndexOf(suffix));
            return a;
        } else if (serverName.indexOf(PREFIX) != -1 && !(serverName.lastIndexOf(suffix) != -1)) {
            return StringUtils.stripStart(serverName, PREFIX);
        } else if (!(serverName.indexOf(PREFIX) != -1) && serverName.lastIndexOf(suffix) != -1) {
            return StringUtils.stripEnd(serverName, suffix);
        } else if (serverName.indexOf("/") != -1) {
            return serverName.split("/")[0];
        } else {
            return serverName;
        }
    }

    public static String join(Collection<?> array,String separator){
        return StringUtils.join(array,separator);
    }

    public static String evaluationJoin(List<?> sourceList,String[] prefix,String suffix,String separator){
        Set<String> results = new HashSet<>(sourceList.size());
        Iterator<?> iterator = sourceList.iterator();
        while(iterator.hasNext()){
            String v = (String) iterator.next();
            results.add(evaluationName(v,prefix,suffix));
        }
        return join(results,separator);
    }

    public static String evaluationName(String source,String[] prefix,String suffix){
        String localPrefix = "";
        for(String p:prefix){
            if(StringUtils.contains(source,p)){
                localPrefix = p;
                break;
            }
        }
        String result = StringUtils.substringBefore(StringUtils.substringAfter(source,localPrefix),suffix);
        if(isNotBlank(result)){
            return result;
        }else {
            return source;
        }
    }

    public static String replaceAll(String s,String s1,String s2){
        return StringUtils.replaceIgnoreCase(s,s1,s2);
    }

    public static String formatDbPathName(String dbPathName) {
        return dbPathName.replaceAll("\\\\", "/");
    }

    public static boolean isBlank(String str) {
        return StringUtils.isBlank(str);
    }


    public static boolean isNotBlank(String str) {
        return StringUtils.isNotBlank(str);
    }

    public static String replace(String s, String s1, String s2) {
        return s.replace(s1, s2);
    }

    public static void main(String[] args) {
//        List<String> servers = Arrays.asList("CN=archive.jx.chinamobile.com/O=jxmobile", "archive.jx.chinamobile.com/jxmobile");
//        servers.forEach(s -> {
//            System.out.println(StringUtil.formatServerName(s, "/O=jxmobile"));
//        });
//        System.out.println(StringUtil.formatDbPathName("lib\\ax.nsf"));

        String[] strings = "CN=李芳/OU=lifang/O=jxmobile,CN=李争/OU=lizheng/O=jxmobile,CN=廖晓明/OU=liaoxiaoming/O=jxmobile,CN=邓琛/OU=dengchen/O=jxmobile,CN=郭建华/OU=guojianhua/O=jxmobile,CN=刘宏志/OU=liuhongzhi/O=jxmobile,CN=李芳/OU=lifang/O=jxmobile,CN=admin/O=jxmobile,CN=邓琛/OU=dengchen/O=jxmobile,CN=admin/O=jxmobile,CN=郭建华/OU=guojianhua/O=jxmobile,CN=admin/O=jxmobile,CN=邓琛/OU=dengchen/O=jxmobile,CN=admin/O=jxmobile".split(",");
        List<String> stringList = Arrays.asList(strings);
        String s = evaluationJoin(stringList,new String[]{"OU=","CN="},"/",",");

    }

}
